package cn.guozeyiblog.blog;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.support.v7.app.ActionBarActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener, OnPageChangeListener {
	private LinearLayout tabMain; //主页
	private LinearLayout tabCat;    //分类
	private LinearLayout tabHot;    //热门
	private LinearLayout tabChat;  //微语
	private ImageView imageView; //中间的"+"
	private TextView tv_tab1; //表示标签TAb下的下划线
	private TextView tv_tab2;
	private TextView tv_tab3;
	private TextView tv_tab4;
	private ViewPager viewPager;
	private View popView;
	private PopupWindow window ;
	private FrameLayout footer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		tv_tab1.setVisibility(View.VISIBLE); //默认显示主页
		tabMain.setOnClickListener(this);
		tabCat.setOnClickListener(this);
		tabHot.setOnClickListener(this);
		tabChat.setOnClickListener(this);
		imageView.setOnClickListener(this);
		MyPagerAdapter adapter = new MyPagerAdapter(MainActivity.this);
		viewPager.setAdapter(adapter);
		viewPager.setOnPageChangeListener(this);
	}

	private void initView(){
		tabMain = (LinearLayout) findViewById(R.id.tab1);
		tabCat = (LinearLayout) findViewById(R.id.tab2);
		tabHot = (LinearLayout) findViewById(R.id.tab3);
		tabChat = (LinearLayout) findViewById(R.id.tab4);
		imageView = (ImageView) findViewById(R.id.image1);
		tv_tab1 = (TextView) findViewById(R.id.tab1_line);
		tv_tab2 = (TextView) findViewById(R.id.tab2_line);
		tv_tab3 = (TextView) findViewById(R.id.tab3_line);
		tv_tab4 = (TextView) findViewById(R.id.tab4_line);
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		footer = (FrameLayout) findViewById(R.id.footer);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		if (window!=null&&window.isShowing()&&v.getId()!=R.id.image1) {
			window.dismiss();
		}
		switch (v.getId()) {
		case R.id.tab1:
			HidenBottomLine();
			tv_tab1.setVisibility(View.VISIBLE);
			viewPager.setCurrentItem(0);
			break;
		case R.id.tab2:
			HidenBottomLine();
			tv_tab2.setVisibility(View.VISIBLE);
			viewPager.setCurrentItem(1);
			break;
		case R.id.tab3:
			HidenBottomLine();
			tv_tab3.setVisibility(View.VISIBLE);
			viewPager.setCurrentItem(2);
			break;
		case R.id.tab4:
			HidenBottomLine();
			tv_tab4.setVisibility(View.VISIBLE);
			viewPager.setCurrentItem(3);
			break;
		case R.id.image1:
			if (window!=null&&window.isShowing()) {
				window.dismiss();
				return ;
			}else {
				imageView.setImageResource(R.drawable.toolbar_plusback);
				showPopupWindows();
			}
			break;
		case R.id.menu1://主页
            //Toast.makeText(getApplicationContext(), "Menu1", 0).show();
			HidenBottomLine();
			viewPager.setCurrentItem(0);
			
			tv_tab1.setVisibility(View.VISIBLE);
			break;
		case R.id.menu2://web主页
			//Toast.makeText(getApplicationContext(), "Menu2", 0).show();	
			Intent hotweb = new Intent(MainActivity.this,Article.class);
			hotweb.putExtra("Uri", "http://www.guozeyiblog.cn");
			startActivity(hotweb);
			break;
		case R.id.menu3://退出
			//Toast.makeText(getApplicationContext(), "Menu3", 0).show();
			finish();
			System.exit(0);
			break;
		case R.id.menu4://设置
			//Toast.makeText(getApplicationContext(), "Menu4", 0).show();
			Intent setting = new Intent(MainActivity.this,Setting.class);
			startActivity(setting);
			break;
		case R.id.menu5://反馈
			//Toast.makeText(getApplicationContext(), "Menu5", 0).show();
			Intent feed = new Intent(MainActivity.this ,Feedback.class);
			startActivity(feed);
			break;
		case R.id.menu6: //关于
			Toast.makeText(getApplicationContext(), "Menu6", 0).show();
			Intent intent = new Intent(getApplicationContext(), AboutMe.class);
			startActivity(intent);
			break;

		default:
			break;
		}

	}

	@Override
	public void onPageScrollStateChanged(int arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageScrolled(int arg0, float arg1, int arg2) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onPageSelected(int arg0) {
		// TODO Auto-generated method stub
		HidenBottomLine();
		switch (arg0) {
		case 0:
			tv_tab1.setVisibility(View.VISIBLE);
			break;
		case 1:
			tv_tab2.setVisibility(View.VISIBLE);
			break;
		case 2:
			tv_tab3.setVisibility(View.VISIBLE);
			break;
		case 3:
			tv_tab4.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}

	private void HidenBottomLine(){
		tv_tab1.setVisibility(View.GONE);
		tv_tab2.setVisibility(View.GONE);
		tv_tab3.setVisibility(View.GONE);
		tv_tab4.setVisibility(View.GONE);
	}

	private void showPopupWindows(){
		if (window==null) {
			LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
			popView = inflater.inflate(R.layout.popupwindow, null);
			window = new PopupWindow(popView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
		}
		WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
		window.setOutsideTouchable(false);
		//window.setFocusable(true);
		//window.setTouchable(true);
		//window.setBackgroundDrawable(new BitmapDrawable());
		window.showAtLocation(imageView, Gravity.BOTTOM, 0, footer.getHeight()-10);
		LinearLayout menu1 = (LinearLayout) popView.findViewById(R.id.menu1);
		LinearLayout menu2 = (LinearLayout) popView.findViewById(R.id.menu2);
		LinearLayout menu3 = (LinearLayout) popView.findViewById(R.id.menu3);
		LinearLayout menu4 = (LinearLayout) popView.findViewById(R.id.menu4);
		LinearLayout menu5 = (LinearLayout) popView.findViewById(R.id.menu5);
		LinearLayout menu6 = (LinearLayout) popView.findViewById(R.id.menu6);
		menu1.setOnClickListener(this);
		menu2.setOnClickListener(this);
		menu3.setOnClickListener(this);
		menu4.setOnClickListener(this);
		menu5.setOnClickListener(this);
		menu6.setOnClickListener(this);
		window.setOnDismissListener(new OnDismissListener() {
			@Override
			public void onDismiss() {
				imageView.setImageResource(R.drawable.toolbar_plus);				
			}
		});
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			if (window!=null&&window.isShowing()) {
				window.dismiss();
			}else {
				AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
				.setTitle("你确定要退出了吗?")
				.setNegativeButton("退出", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						finish();
						System.exit(0);
					}
				})
				.setPositiveButton("继续", null)
				.create();
				dialog.show();
			}
			break;
		case KeyEvent.KEYCODE_MENU:
			if (window!=null&&window.isShowing()) {
				window.dismiss();
			}else {
				imageView.setImageResource(R.drawable.toolbar_plusback);
				showPopupWindows();
			}
		default:
			break;
		}
		return true;
	}

}
